//
//  YYCarDetailVC.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/8/19.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "YYCarDetailVC.h"
#import "SDCycleScrollView.h"
#import "YYSubscribeView.h"
#import "YYSpellGroupDetailVC.h"

@interface YYCarDetailVC ()<SDCycleScrollViewDelegate,UIWebViewDelegate>
@property (strong, nonatomic) SDCycleScrollView *adCycleScrollView;

@property (nonatomic,strong)NSDictionary * dataDic;

@end

@implementation YYCarDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.wevView.scrollView.scrollEnabled = NO;
    self.lbTag.layer.borderWidth = 0.75;
    self.lbTag.layer.borderColor = [UIColor colorWithHexString:@"fb2a2a"].CGColor;
    self.lbTag.layer.cornerRadius = 2;
    self.lbTag.layer.masksToBounds = YES;
    self.wevView.delegate = self;
    [self getData];
}


-(void)getData{
    [RequestOperationManager carDetail_id:self.carId success:^(id result) {
        if (DEF_REQ_SUCCESS) {
            self.dataDic = [result[@"data"] copy];
            [self reloadData];
        }
        
    } failture:^(NSError *error) {
        
    }];
}


-(void)reloadData{
    self.lbName.text = [NSString stringWithFormat:@"%@ %@",self.dataDic[@"brand"],self.dataDic[@"vehicle_type"]];
    self.lbSeller.text = self.dataDic[@"seller_name"];
    self.lbAdress.text = self.dataDic[@"seller_address"];
    if ([self.dataDic[@"benefit_money"] length]>0) {
        self.lbPrice.text = [NSString stringWithFormat:@"%@",self.dataDic[@"benefit_money"]];
        self.OPLine.hidden = NO;
        self.lbOldPrice.hidden = NO;
        self.lbOldPrice.text =[NSString stringWithFormat:@"新车原价￥%@",self.dataDic[@"money"]];
    }else{
        self.OPLine.hidden = YES;
        self.lbOldPrice.hidden = YES;
        self.lbPrice.text = [NSString stringWithFormat:@"%@",self.dataDic[@"money"]];
    }

    NSArray * bannerArr = self.dataDic[@"vehicle_img"];
    NSMutableArray * imageArr = [NSMutableArray arrayWithCapacity:0];
    NSMutableArray * titleArr = [NSMutableArray arrayWithCapacity:0];
    for (int i =0; i<bannerArr.count; i++) {
        [imageArr addObject:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,bannerArr[i]]];
        [titleArr addObject:[NSString stringWithFormat:@"%i/%lu",i+1,(unsigned long)bannerArr.count]];
    }
    self.adCycleScrollView.titlesGroup = titleArr;
    self.adCycleScrollView.imageURLStringsGroup =imageArr;
    self.lbChange.text = [NSString stringWithFormat:@"%@过户",self.dataDic[@"transfer"]];

    self.label1.text = self.dataDic[@"type_id"];//车型
    self.lb2.text =[NSString stringWithFormat:@"%@",self.dataDic[@"pay"]];//首付
    self.lb3.text = self.dataDic[@"monthly"];//月供
    self.lb4.text = self.dataDic[@"change"];//变速箱
    self.lb5.text = self.dataDic[@"inside"];//内饰
    self.lb6.text = self.dataDic[@"external"];//外观
    self.lb7.text = self.dataDic[@"move"];//环保标准
    self.lb8.text = self.dataDic[@"engine"];//发动机
    self.lb9.text = self.dataDic[@"discharge"];//排放量
    [self.wevView loadHTMLString:self.dataDic[@"content"] baseURL:nil];
}

- (IBAction)call:(id)sender {
    NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@",self.dataDic[@"seller_phone"]];
    if (@available(iOS 10.0, *)) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
    } else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
    }
}
- (IBAction)lookCar:(id)sender {
    [YYSubscribeView sharedInstance].vehicle_id = self.carId;
     [YYSubscribeView sharedInstance].seller_id = self.dataDic[@"seller_id"];
    [YYSubscribeView show];
}

-(NSDictionary *)dataDic{
    if (!_dataDic) {
        _dataDic = [NSDictionary dictionary];
    }
    return _dataDic;
}

-(SDCycleScrollView *)adCycleScrollView{
    if (!_adCycleScrollView) {
        _adCycleScrollView= [SDCycleScrollView cycleScrollViewWithFrame:self.bannerView.bounds delegate:self placeholderImage:[UIImage new]];
        _adCycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleNone;
        _adCycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _adCycleScrollView.titleLabelTextAlignment = NSTextAlignmentRight;
        _adCycleScrollView.titleLabelBackgroundColor = [UIColor clearColor];
        _adCycleScrollView.contentMode = UIViewContentModeScaleAspectFill;
        [self.bannerView addSubview:_adCycleScrollView];
        [_adCycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
        }];
        
    }
    return _adCycleScrollView;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString *js = @"function imgAutoFit() { \
    var imgs = document.getElementsByTagName('img'); \
    for (var i = 0; i < imgs.length; ++i) {\
    var img = imgs[i];   \
    img.style.maxWidth = %f;   \
    } \
    }";
    js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];
    
    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];
    self.webViewHeight.constant = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue]+80;
}

@end
